package dwei.core.load.proxy
{
	import dwei.core.load.LoadType;

	public class LoadProxyFactory
	{
		private static const _typeProxyMap:Object = {};
		private static const _types:Array = [
			LoadType.BINARY, LoadType.DISPLAYABLE, LoadType.MODULE, LoadType.SOUND, LoadType.TEXT
		];
		public static function getProxy(type:String):ILoadProxy
		{
			if (_types.indexOf(type) == -1) type = LoadType.BINARY;
			var pool:Array;
			if (_typeProxyMap.hasOwnProperty(type))
				pool = _typeProxyMap[type];
			else
			{
				pool = [];
				_typeProxyMap[type] = pool;
			}
			
			var proxy:ILoadProxy;
			for each (proxy in pool)
			{
				if (!proxy.isLoading) return proxy;
			}
			switch (type)
			{
				case LoadType.TEXT:
					proxy = new LoadText();
					break;
				case LoadType.SOUND:
					proxy = new LoadSound();
					break;
				case LoadType.MODULE:
					proxy = new LoadModule();
					break;
				case LoadType.BINARY:
					proxy = new LoadBinary();
					break;
				case LoadType.DISPLAYABLE:
					proxy = new LoadDisplayable();
					break;
			}
			pool.push(proxy);
			return proxy;
		}
	}
}